#!groovy

// This "basic pipeline" runs quick tests:
// * builds a tarball as usual
// * uses that tarball for
//   + running Unix checks
//   + building with MSVC on Windows
//
// The GIT checkout must use the remote branch name as the checkout local
// branch name so that tarball names contain the branch name.
// In "Additional Behaviours", enable "Checkout to specific local branch"
// and leave "Branch name" empty.
// Not needed for multi-branch pipelines which already BRANCH_NAME in the environment.

def gitBranch = ""
def tarballgz
def tarballbz2
def statusHasChanged = false

pipeline {
	agent none

	stages {
		stage('Tarball') {
			steps {
				node('autotools') {
					checkout scm
					script {
						gitBranch = sh (script: 'if test "$BRANCH_NAME"; then echo $BRANCH_NAME; else git rev-parse --abbrev-ref HEAD; fi', returnStdout: true).trim()
					}
					sh 'contrib/ci.inria.fr/job-0-tarball.sh '+gitBranch
					script {
					       tarballgz = sh (script: 'ls *.gz', returnStdout: true).trim()
					       tarballbz2 = sh (script: 'ls *.bz2', returnStdout: true).trim()
					}
					stash includes: tarballgz, name: 'tarballgz'
					// Stash those scripts because they are not in make dist
					dir('contrib/ci.inria.fr') {
						stash includes: "job-1-check.sh", name: 'script-unix-check'
						stash includes: "job-1-visualstudio.bat", name: 'script-msvc'
					}
					archiveArtifacts artifacts: tarballgz+","+tarballbz2+",doc/doxygen-doc/hwloc-a4.pdf", fingerprint: true, onlyIfSuccessful: true
					deleteDir()
				}
			}
		}
		stage('Check') {
			steps {
				script {
					listOfNodeNames = []
					if (env.NO_UNIX != 'true') {
						listOfNodeNames = nodesByLabel('unix')
					}
					if (env.NO_MSVC != 'true') {
						listOfNodeNames.push('VisualStudio')
					}
					def p = listOfNodeNames.collectEntries {
					[ (it): {
						if (it != 'VisualStudio') {
							node(it) {
								dir('check-unix') {
									unstash 'tarballgz'
									unstash 'script-unix-check'
									sh 'chmod 755 job-1-check.sh && ./job-1-check.sh '+tarballgz
									if (env.KEEP_WORKING_DIRECTORY != 'true')
										deleteDir()
								}
							}
						} else {
							node('msvc') {
								dir('check-msvc') {
									unstash 'tarballgz'
									unstash 'script-msvc'
									bat 'job-1-visualstudio.bat '+tarballgz
									if (env.KEEP_WORKING_DIRECTORY != 'true')
										deleteDir()
								}
							}
						}
					}]}
					parallel p;
				}
			}
		}
	}

	post {
		// hooks are called in order: always, changed, aborted, failure, success, unstable
		changed {
			echo "Build status has changed."
			script {
				statusHasChanged = true
			}
		}
		success {
			echo "Build success."
			// email when changed to success
			script {
				if (statusHasChanged || env.EMAIL_NOTIFICATION == 'true') {
					emailext(body: '${DEFAULT_CONTENT}',
						 subject: '${DEFAULT_SUBJECT}',
						 replyTo: '$DEFAULT_REPLYTO',
						 to: '$DEFAULT_RECIPIENTS',
						 recipientProviders: [[$class: 'CulpritsRecipientProvider'],[$class: 'RequesterRecipientProvider']])
				}
			}
		}
		failure {
			echo "Build failure."
			// always email on failure
			emailext(body: '${DEFAULT_CONTENT}',
				 subject: '${DEFAULT_SUBJECT}',
				 replyTo: '$DEFAULT_REPLYTO',
				 to: '$DEFAULT_RECIPIENTS',
				 recipientProviders: [[$class: 'CulpritsRecipientProvider'],[$class: 'RequesterRecipientProvider']])
		}
        }
}
